<?php
/**
 * Prado Portal.
 *
 * @author Steen Rabol <steen.rabol@gmail.com>
 * @link http://www.pradoportal.dk/
 * @copyright Copyright &copy; 2006,2007,2008 Steen Rabol
 * @license http://www.pradoportal.dk
 * @version $Id: ThemeManCssImport.php 346 2010-01-31 10:22:06Z steen.rabol@gmail.com $
 *
 */

Prado::using('Application.Common.Data.PortalThemeRecord');
Prado::using('Application.Common.Data.PortalThemeCssClassRecord');
Prado::using('Application.Common.Data.PortalThemeCssClassAttrRecord');
Prado::using('Application.Common.PortalParseCss');

class ThemeManCssImport extends PortalAdminPage
{
	private $_themeid;
	private $_themerec	= null;

	public function setThemeId($value)
	{
		$this->_themeid	= $value;
	}

	public function getThemeId()
	{
		return $this->_themeid;
	}

	public function setThemeRec($value)
	{
		$this->_themerec	= $value;
	}

	public function getThemeRec()
	{
		return $this->_themerec;
	}

	public function dataBind()
	{
	}

	public function onInit($param)
	{
		parent::onInit($param);

		$this->ThemeId					= TPropertyValue::ensureInteger($this->Request['id']);
		$this->ThemeRec					= PortalThemeRecord::finder()->findByid($this->ThemeId);
		$this->FileUpload->MaxFileSize	= PortalUtil::return_bytes(ini_get('upload_max_filesize'));
		$this->ltFileSize->Text			= ini_get('upload_max_filesize');
		$this->setAdminPageTitle('Import Css');
		$this->FileUpload->setAttribute("size",30);
	}

	public function onLoad($param)
	{
		parent::onLoad($param);

		if(!$this->IsPostBack)
		{
			$this->dataBind();
		}
	}

	public function onFileUpload($sender,$param)
	{
		if($sender->HasFile)
		{
			PortalUtil::CreateDirStructure($this->Application->PortalBasePath	. '/storage/tmp');

			$savepath	= $this->Application->PortalBasePath	. '/storage/tmp/' . $sender->FileName;
			$sender->saveAs($savepath);

			if(TPropertyValue::ensureInteger($this->DeleteFirst->Checked))
			{
				$this->DeleteCssClasses();
			}
			$cssParse	= new PortalParseCss();
			$cssClasses	= $cssParse->parseFile($savepath);

			if(is_array($cssClasses) && count($cssClasses) > 0)
			{
				foreach($cssClasses as $key => $cssClass)
				{
					$cssClassRec	= PortalThemeCssClassRecord::finder()->findBythemeid_AND_name($this->ThemeId,$key);

					if($cssClassRec === null)
					{
						$cssClassRec			= new PortalThemeCssClassRecord();
						$cssClassRec->themeid	= $this->ThemeId;
						$cssClassRec->name		= trim($key);
						$cssClassRec->active	= 1;
						$cssClassRec->save();
					}

					// Save attributes
					if(is_array($cssClass) && count($cssClass) > 0)
					{
						foreach($cssClass as $aKey => $aValue)
						{
							$cssAttrRec	= PortalThemeCssClassAttrRecord::finder()->findBytcid_AND_name($cssClassRec->id,$aKey);

							if($cssAttrRec === null)
							{
								$cssAttrRec	= new PortalThemeCssClassAttrRecord();
								$cssAttrRec->tcid	= $cssClassRec->id;
								$cssAttrRec->name	= trim($aKey);
							}

							$cssAttrRec->attr	= trim($aValue);
							$cssAttrRec->save();
						}
					}
				}
			}
		}

		$this->gotoPage('Admin.ThemeMan');
	}

	private function DeleteCssClasses()
	{
		$cssClasses = PortalThemeCssClassRecord::finder()->findAllBythemeid($this->ThemeId);

		if($cssClasses !== null)
		{
			foreach($cssClasses as $cClass)
			{
				$cClass->delete();
			}
		}
	}
}

?>